package network3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class recevice {
    public static void main(String[] args) {
        try {
            //创建接收端的socket对象
            DatagramSocket socket = new DatagramSocket(12345);
            //死循环接收
            while (true) {
                byte[] bytes = new byte[1024];
                DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
                socket.receive(packet);
                //接收数据
                String IP = packet.getAddress().getHostAddress();
                String s = new String(packet.getData(), 0, packet.getLength());
                System.out.println("从" + IP + "接收:" + s);
            }
            //要一直接收数据 所以不关闭
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
